package edu.bbu.commonproject.desktop.gui.controller.utils;

/**
 * The string formatter.
 */
public class StringFormatter
{
	/**
	 * Delete all whiteSpaces from the user name and converting it to lower case.
	 * @param userName the user name.
	 * @return the formatted string.
	 */
	public String userNameFormat(String userName)
	{		
		if (userName != null)
		{
			return userName.replaceAll("\\s+", "").toLowerCase();
		}
		
		return null;
	}
	
	/**
	 * The parent directory formatter.<br>
	 * This will cut the last directory from absolute path.<p>
	 * Example "C:\\parent\child\" ->  "C:\\parent\"
	 * @param directory the absolute directory path.
	 * @return the parent directory.
	 */
	public String parentDirectoryFormat(String directory, String seprator)
	{
		directory = directory.substring(0, directory.lastIndexOf(seprator));
		
		int lastBackSlash = directory.lastIndexOf(seprator);
		
		if(lastBackSlash != -1)
		{
			return directory.substring(0, lastBackSlash + 1);
		}
		else 
		{
			return "";
		}
	}
	
	/**
	 * Return the last directory name in the path.
	 * Example "C:\\parent\child\" ->  "child"
	 * @param path the server directory path.
	 * @return the last directory name in the path.
	 */
	public String getLastDirectoryInPath(String path, String seprator)
	{
		int lastBackSlash = path.lastIndexOf(seprator);
		
		if(lastBackSlash == -1)
		{
			return "";
		}
		
		path = path.substring(0, lastBackSlash);
		
		lastBackSlash = path.lastIndexOf(seprator);
		
		if(lastBackSlash == -1)
		{
			return "";
		}
		
		return path.substring(lastBackSlash + 1, path.length());
	}
	
	/**
	 * Checks if the directory contains invalid characters.<br>
	 * Invalid characters are:<br><br>
	 * <b>"  *  /  :  <  >  ?  \  |</b>
	 * @param directory the directory name.
	 * @return true if directory name is valid.
	 */
	public boolean isValiadDirectoryName(String directory)
	{
		if(directory.length() == 0)
		{
			return false;
		}
		
		if(directory.contains("\""))
		{
			return false;
		}
		
		if(directory.contains("*"))
		{
			return false;
		}
		
		if(directory.contains("/"))
		{
			return false;
		}
		
		if(directory.contains(":"))
		{
			return false;
		}
		
		if(directory.contains("<"))
		{
			return false;
		}
		
		if(directory.contains(">"))
		{
			return false;
		}
		
		if(directory.contains("?"))
		{
			return false;
		}
		
		if(directory.contains("\\"))
		{
			return false;
		}
		
		if(directory.contains("|"))
		{
			return false;
		}
		
		return true;
	}
}
